<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/9/4
 * Time: 9:36
 */

namespace app\admin\controller;


use app\admin\model\Cash;
use app\admin\model\Detail;
use app\admin\model\Members;
use app\admin\model\Recharge;
use app\home\controller\Biapi;
use think\Db;
use think\Request;

class Money extends Admin
{
    /**
     * 充值记录
     */
    public function recharge(Request $request){

        $from   = $request->param('from');
        $to     = $request->param('to');
        $status = $request->param('status');
        $mobile = $request->param('mobile');
        $w='';
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['create_at'] = [['>=',$from],['<=',$to]];
        }
        if($status){
            $w['status']=$status;
        }
        if($mobile){
            $uid = Members::where(array('mobile'=>$mobile))->value('id');
            $w['uid'] = $uid;
        }
        $list = Recharge::where($w)->order('id desc')->paginate(20,false,['query' => request()->param()]);
        $renshu      = Recharge::where($w)->count();
        $totle_money = Recharge::where($w)->sum('money');
        return view('',[
            'list'=>$list,
            'page'=>$list->render(),
            'renshu'=>$renshu,
            'total_money'=>$totle_money,
        ]);
    }

    /**
     *  后台充值
     */
    public function addmoney(Request $request){

        $mobile   = $request->post('mobile');
        $money    = $request->post('money');
        $remark   = $request->post('remark');
        $Members = new Members();
        $uid = $Members->get_mobile_id($mobile);
        if(!$uid){
            $this->error_new('用户不存在');
        }
        Db::startTrans();
        try {
            $up_data["money"] = array("exp", "money+" . $money);
            $result0 = Db::name('member')->where(array('id'=>$uid))->update($up_data);
            $Recharge = new Recharge();
            $data['uid']    = $uid;
            $data['remark'] = $remark;
            $data['money']  = $money;
            $data['status'] = 2;
            $data['type']   = 3;
            $data['create_at'] = time();
            $result1 = $Recharge->addData($data);
            $this->add_charge_detail($uid,$money,1);
            Db::commit();
            $this->success_new('充值成功',url('money/recharge'));
        }catch (\Exception $e){
            Db::rollback();
            $this->error_new('充值失败');
        }

    }

    /**
     * 修改充值状态
     */
    public function edit_status(Request $request){
        $type   = $request->post('type');
        $uid    = $request->post('uid');
        $id     = $request->post('id');
        Db::startTrans();
        try{
            if($type==1) {
                $result_up = Db::name('recharge')->where(array('id' =>$id))->update(array('status' => 2,'update_at'=>time()));
                $money     = Db::name('recharge')->where(array('id' =>$id))->value('money');
                $up_data["money"] = array("exp", "money+" . $money);
                $result = Db::name('member')->where(array('id'=>$uid))->update($up_data);
                $this->add_charge_detail($uid,$money,1);
           }
            if($type==2) {
                $result_up = Db::name('recharge')->where(array('id' =>$id))->update(array('status' => 3,'update_at'=>time()));
            }
            Db::commit();
            json_return(200,'成功');
        }catch (\Exception $e){
            Db::rollback();
            json_return(500,'失败');
        }
    }

    /**
     * 提现
     */
    public function cash(Request $request){
        $mobile    = $request->param('mobile');
        $status    = $request->param('status');
        $from      = $request->param('from');
        $to        = $request->param('to');
        $uid='';
        if($mobile){
            $Members = new Members();
            $uid = $Members->get_mobile_id($mobile);
            if(!$uid){
                $this->error_new('用户不存在');
            }
        }
        $w=[];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['create_at'] = [['>=',$from],['<=',$to]];
        }
        if($status){
            $w['status']=$status;
        }
        if($uid){
            $w['uid']=$uid;
        }
        $list        = Cash::where($w)->order('id desc')->paginate(20,false,['query' => request()->param()]);
        $renshu      = Cash::where($w)->count();
        $totle_money = Cash::where($w)->sum('money');
        return view('',[
            'list'=>$list,
            'page'=>$list->render(),
            'renshu'=>$renshu,
            'total_money'=>$totle_money,
        ]);
    }

    /**
     * 修改提现记录
     */
    public function edit_withdrawal_status(Request $request){
        $type   = $request->post('type');
        $uid    = $request->post('uid');
        $id     = $request->post('id');
        Db::startTrans();
        try{
            if($type==1) {
                $result_up = Db::name('withdrawals')->where(array('id' =>$id))->update(array('status' => 2,'update_at'=>time()));
            }
            if($type==2) {
                $result_up = Db::name('withdrawals')->where(array('id' =>$id))->update(array('status' => 3,'update_at'=>time()));
                $money     = Db::name('withdrawals')->where(array('id' =>$id))->value('money');
                $result   = Db::name('member')->where(array('id'=>$uid))->setInc('money',$money);
            }
            Db::commit();
            json_return(200,'成功');
        }catch (\Exception $e){
            Db::rollback();
            json_return(500,'失败');
        }
    }

    /**
     * 金额记录
     */
    public function transaction(Request $request){
        $from    = $request->param('from');
        $to      = $request->param('to');
        $id      = $request->param('id');
        $cate    = $request->param('exp');
        $mobile  = $request->param('mobile');
        $w =[];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['create_at'] = [['>=',$from],['<=',$to]];
        }
        if($cate){
            $w['exp'] = $cate;
        }
        if($id){
            $w['uid']  = $id;
        }
        if($mobile){
            $Members = new Members();
            $uid = $Members->get_mobile_id($mobile);
            if(!$uid){
                $this->error_new('用户不存在');
            }else{
                $w['uid']  = $uid;
            }
        }
        $list = Detail::where($w)->order('id desc')->paginate(20,false,['query' => request()->param()]);
        $exps = Db::name('detail_exp')->select();
        return view('',[
            'list' =>$list,
            'id'   =>$id,
            'page' =>$list->render(),
            'exps' =>$exps
        ]);

    }

    /**
     * 修改流水状态
     */
    public function edit_water_status(Request $request){
        $type   = $request->post('type');
        $uid    = $request->post('uid');
        $id     = $request->post('id');

        Db::startTrans();
        try{
            if($type==1) {
                $result_up = Db::name('detail')->where(array('id' =>$id))->update(['status' => 2,'update_at'=>time()]);
                $money     = Db::name('detail')->where(array('id' =>$id))->value('money');
                $up_data["money"]    = array("exp", "money+" . $money);
                $result = Db::name('member')->where(array('id'=>$uid))->update($up_data);
            }
            if($type==2) {
                $result_up = Db::name('detail')->where(array('id' =>$id))->update(['status' => 3,'update_at'=>time()]);
            }
        }catch (\Exception $e){
            Db::rollback();
            json_return(500,'失败');
        }
        if($result_up){
            Db::commit();
            json_return(200,'成功');
        }else{
            Db::rollback();
            json_return(500,'失败');
        }
    }

    /**
     * 检查是否有新的充值
     */
    public function is_recharge(){
        $re = Db::name('recharge')
            ->where('is_read',0)
            ->order('id desc')
            ->find();
        $rs = Db::name('withdrawals')
            ->where('is_read',0)
            ->order('id desc')
            ->find();
        if($re || $rs){
            if($re) {
                $name = Db::name('member')->where('id',$re['uid'])->value('mobile');
                Db::name('recharge')
                    ->where(array('is_read' => 0))
                    ->update(array('is_read' => 1));
                $data['type'] = 1;
                $data['name'] = $name;
            }
            if($rs){
                $name = Db::name('member')->where('id',$rs['uid'])->value('mobile');
                Db::name('withdrawals')
                    ->where(array('is_read' => 0))
                    ->update(array('is_read' => 1));
                $data['type'] = 2;
                $data['name'] = $name;
            }
            json_return(200, '成功',$data);
        }else{
            $data['type'] = 0;
            $data['name'] = '无';
            json_return(500,'成功',$data);
        }
    }

    /**
     * 发红包记录
     */
    public function send_red(Request $request){
        $from    = $request->param('from');
        $to      = $request->param('to');
        $mobile  = $request->param('mobile');
        $w =[];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['r.create_at'] = [['>=',$from],['<=',$to]];
        }

        if($mobile){
            $Members = new Members();
            $uid = $Members->get_mobile_id($mobile);
            if(!$uid){
                $this->error_new('用户不存在');
            }else{
                $w['r.uid']  = $uid;
            }
        }
        $list = Db::name('red')
            ->alias('r')
            ->field('r.*,m.nickname,m.mobile')
            ->join('dl_member m','r.uid=m.id')
            ->where($w)
            ->order('id desc')
            ->paginate(20,false,['query' => request()->param()]);
        return view('',[
            'list' =>$list,
            'page' =>$list->render(),

        ]);
    }

    /**
     * 抢红包记录
     */
    public function rob_red(Request $request){
        $from    = $request->param('from');
        $to      = $request->param('to');
        $mobile  = $request->param('mobile');
        $w =[];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['r.create_at'] = [['>=',$from],['<=',$to]];
        }

        if($mobile){
            $Members = new Members();
            $uid = $Members->get_mobile_id($mobile);
            if(!$uid){
                $this->error_new('用户不存在');
            }else{
                $w['r.uid']  = $uid;
            }
        }
        $list = Db::name('red_rob')
            ->alias('r')
            ->field('r.*,m.nickname,m.mobile')
            ->join('dl_member m','r.uid=m.id')
            ->where($w)
            ->order('id desc')
            ->paginate(20,false,['query' => request()->param()]);
        return view('',[
            'list' =>$list,
            'page' =>$list->render(),

        ]);
    }

    /**
     * 礼物记录
     */
    public function gift_detail(Request $request){
        $from    = $request->param('from');
        $to      = $request->param('to');
        $mobile  = $request->param('mobile');
        $w =[];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['o.create_at'] = [['>=',$from],['<=',$to]];
        }

        if($mobile){
            $Members = new Members();
            $uid = $Members->get_mobile_id($mobile);
            if(!$uid){
                $this->error_new('用户不存在');
            }else{
                $w['o.uid']  = $uid;
            }
        }
        $list = Db::name('order')
            ->alias('o')
            ->field('o.*,m.nickname,m.mobile,g.gift_name')
            ->join('dl_member m','o.uid=m.id')
            ->join('dl_gift g','g.id=o.gift_id')
            ->where($w)
            ->order('id desc')
            ->paginate(20,false,['query' => request()->param()]);
        $data = $list->all();
        foreach ( $data as $k=>$v){
            $r_info  = Db::name('member')->field('mobile,nickname')->where('id',$v['r_uid'])->find();
            $v['r_mobile']   = $r_info['mobile'];
            $v['r_nickname'] = $r_info['nickname'];
            $list[$k] = $v;
        }
        return view('',[
            'list' =>$list,
            'page' =>$list->render(),

        ]);
    }

}